package com.dev.web.controller.safe;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dev.bbs.domain.BbsComment;
import com.dev.bbs.service.IBbsCommentService;
import com.dev.common.annotation.Log;
import com.dev.common.core.controller.BaseController;
import com.dev.common.core.domain.AjaxResult;
import com.dev.common.enums.BusinessType;
import com.dev.common.utils.poi.ExcelUtil;
import com.dev.framework.util.ShiroUtils;

/**
 * 帖子评论Controller
 *
 * @author dev
 * @date 2019-09-23
 */
@Controller
@RequestMapping("/safe/comment")
public class SafeCommentController extends BaseController
{
    @Autowired
    private IBbsCommentService bbsCommentService;

    /**
     * 导出帖子评论列表
     */
    @RequiresPermissions("bbs:comment:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BbsComment bbsComment)
    {
        List<BbsComment> list = bbsCommentService.selectBbsCommentList(bbsComment);
        ExcelUtil<BbsComment> util = new ExcelUtil<BbsComment>(BbsComment.class);
        return util.exportExcel(list, "comment");
    }

    /**
     * 新增保存帖子评论
     */
    @Log(title = "帖子评论", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BbsComment bbsComment)
    {
        long userId = ShiroUtils.getUserId();
        return toAjax(bbsCommentService.insertBbsComment(bbsComment.setAuthor(userId)));
    }

    /**
     * 修改保存帖子评论
     */
    @Log(title = "帖子评论", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BbsComment bbsComment)
    {
        return toAjax(bbsCommentService.updateBbsComment(bbsComment));
    }

    /**
     * 点赞帖子评论
     */
    @Log(title = "帖子评论", businessType = BusinessType.UPDATE)
    @PostMapping("/like/{mentId}")
    @ResponseBody
    public AjaxResult like(@PathVariable(value = "mentId") Long mentId)
    {
        long userId = ShiroUtils.getUserId();
        return toAjax(bbsCommentService.like(mentId, userId));
    }

    /**
    * 删除帖子评论
    */
    @Log(title = "帖子评论", businessType = BusinessType.DELETE)
    @PostMapping("/remove/{mentId}")
    @ResponseBody
    public AjaxResult remove(@PathVariable(value = "mentId") Long mentId)
    {
        return toAjax(bbsCommentService.deleteBbsCommentById(mentId));
    }
}
